const app = getApp();
var openid = wx.getStorageSync('openid');
var getOpenidUrl = app.config.serverCfg.domain.myOpenid;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,//识别是否已经登录，false为没有登录
    bindMobile:'>',
  },
  /**
   * 用户授权
   */
  doAuthorization: function(e) {
    wx.showToast({
      title: '登录中...',
      icon: 'none',
      duration: 2000
    })
    var that = this;
    if (e.detail.userInfo == null) {
      console.log('user refluse anthorization')
    } else {
      wx.login({
        success: function(res) {
          wx.request({
            url: getOpenidUrl + res.code,
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {
              if (res.data.retcode != 2000000) {
                console.log('sdf')
                return false;
              } else {
                console.log('234324')
                //OK
                wx.hideToast({});
                openid = res.data.data;
                wx.setStorageSync('openid', res.data.data);
                that.loadUserInfo();
              }
            }
          });
        }
      })
    }
  },
  /**
   * 用户信息获取
   */
  loadUserInfo: function() {
    var that = this;
    if (openid != '') {
      wx.getUserInfo({
        success: res => {
          var data = {
            "openid": openid,
            "user": res.userInfo,
          }
          that.setData({
            userInfo: app.globalData.userInfo,
            hasUserInfo: true,
          })
          if (this.userInfoReadyCallback) {
            this.userInfoReadyCallback(res);
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (!wx.getStorageSync('openid')) {
      //如果没有openid直接返回，无论是否有用户缓存信息
      this.setData({
        userInfo: {},
        hasUserInfo: false,
      })
      return false;
    }
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   * 显示一些基本的用户数据
   */
  onShow: function() {
    var that = this;
    openid = wx.getStorageSync('openid');
    if(!openid) {
      that.setData({
        userInfo: {},
        hasUserInfo: false,
      })
      return false;
    }
    if (openid) {
      wx.request({
        url: app.config.serverCfg.domain.myInfo,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: {openid:openid},
        success(res) {
          if (res.data.retcode == 2000000) {
            if (res.data.data.bindMobile == true) {
              that.setData({bindMobile:'已绑定'})
            }
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if (!wx.getStorageSync('openid')) {
      //如果没有openid直接返回，无论是否有用户缓存信息
      this.setData({
        userInfo: {},
        hasUserInfo: false,
      })
      return false;
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**如果没有登录不进行资料跳转 */
  setProfile: function(e) {
    if (openid == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.navigateTo({
      url: '/pages/my/profile/profile'
    });
  },
  /**如果没登录不进行反馈跳转 */
  feedback: function(e) {
    openid = wx.getStorageSync('openid');
    if (openid == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.navigateTo({
      url: '/pages/my/feedback/feedback'
    });
  },
  setting: function(e) {
    wx.navigateTo({
      url: '/pages/my/setting/setting'
    });
  },
  mobile: function (e) {
    openid = wx.getStorageSync('openid');
    if (openid == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.navigateTo({
      url: '/pages/my/mobile/mobile'
    });
  },
  //我的点赞
  myLikePage:function(e) {
    openid = wx.getStorageSync('openid');
    if (openid == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.navigateTo({
      url: '/pages/my/mine/mine?type=1'
    });
  },
  //我的评论
  myCommentPage: function (e) {
    openid = wx.getStorageSync('openid');
    if (openid == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.navigateTo({
      url: '/pages/my/mine/mine?type=2'
    });
  },
  //我的动态
  myDongtaiPage: function (e) {
    openid = wx.getStorageSync('openid');
    if (openid == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.navigateTo({
      url: '/pages/my/mine/mine?type=3'
    });
  },
})